package net.oschina.android.ui.activity;

import java.util.ArrayList;

import net.oschina.android.R;
import net.oschina.android.adapter.NavDrawerListAdapter;
import net.oschina.android.model.NavDrawItem;
import net.oschina.android.ui.fragment.DiscussionsFragment;
import net.oschina.android.ui.fragment.NewsFragment;
import net.oschina.android.ui.fragment.lastestBlogFragment;
import net.oschina.android.ui.fragment.tweetFragment;
import android.annotation.SuppressLint;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
/**
 * @author HuangWenwei
 *
 * @date 2014年8月30日
 */
@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity{
	
	public class SlideMenuClickListener implements ListView.OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			displayView(position);
		}
	}
	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;
	private CharSequence mDrawerTitle;	//nav drawer title
	private CharSequence mTitle;	//used to store app title
	private String[] navMenuTitles;	//slide menu items
	private TypedArray navMenuIcons;
	private ArrayList<NavDrawItem> navDrawerItems;
	private NavDrawerListAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mTitle = mDrawerTitle = getTitle();
		//load slide menu items
		navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
		//nav drawer icons from resources
		navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
		navDrawerItems = new ArrayList<NavDrawItem>();
		
		navDrawerItems.add(new NavDrawItem(navMenuTitles[0],navMenuIcons.getResourceId(0, -1)));
		navDrawerItems.add(new NavDrawItem(navMenuTitles[1],navMenuIcons.getResourceId(1, -1)));
		navDrawerItems.add(new NavDrawItem(navMenuTitles[2],navMenuIcons.getResourceId(2, -1)));
		navDrawerItems.add(new NavDrawItem(navMenuTitles[3],navMenuIcons.getResourceId(3, -1)));
		navDrawerItems.add(new NavDrawItem(navMenuTitles[4],navMenuIcons.getResourceId(4, -1),"0",true));
		
		navMenuIcons.recycle();
		SlideMenuClickListener sc = new SlideMenuClickListener();
		if (sc!=null && mDrawerList!=null) {
			mDrawerList.setOnItemClickListener(sc);
		}
		adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
		mDrawerList.setAdapter(adapter);
		
		//getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);
		
		mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,
				R.drawable.ic_drawer,
				R.string.app_name,
				R.string.app_name){
			public void onDrawerClose(View view){
				getActionBar().setTitle(mDrawerTitle);
				invalidateOptionsMenu();//calling onPrepareOptionMenu() to show action bar icon
			}
			public void onDrawerOpened(View view){
				getActionBar();
				invalidateOptionsMenu();
			}
			
			public void onDrawerSlide(View drawerView,float sliderOffset){
				if(sliderOffset > .55 && !mDrawerLayout.isDrawerOpen(mDrawerList)){
					onDrawerOpened(drawerView);
				}else if(sliderOffset< .45 && mDrawerLayout.isDrawerOpen(mDrawerList)){
					onDrawerClose(drawerView);
				}
			}
		};
		mDrawerLayout.setDrawerListener(mDrawerToggle);
		if(savedInstanceState == null){
			//on first time display view for first nav item
			displayView(1);
		}
	}
	
	/**
	 *2014年8月30日
	 *@param i
	 */
	@SuppressLint("Recycle")
	private void displayView(int i) {
		Fragment fg = null;
		switch (i) {
		case 0:
			fg = new lastestBlogFragment();
			break;
		case 1:
			fg = new NewsFragment();
			break;
		case 2:
			fg = new DiscussionsFragment();
			break;
		case 3:
			fg = new tweetFragment();
			break;
		default:
			break;
		}
		if(fg!=null){
			FragmentManager fragmentManager = getSupportFragmentManager();
			fragmentManager.beginTransaction().replace(R.id.frame_container, fg).commit();
			mDrawerList.setItemChecked(i, true);
			mDrawerList.setSelection(i);
			setTitle(navMenuTitles[i]);
			mDrawerLayout.closeDrawer(mDrawerList);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(mDrawerToggle.onOptionsItemSelected(item)){
			return true;
		}
		switch (item.getItemId()) {
		case R.string.menu_tweet:
			return true;
		case R.string.menu_post:
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
		menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	@Override
	public void setTitle(CharSequence title) {
		mTitle  = title;
		getActionBar().setTitle(title);
	}
}
